<?php
// +----------------------------------------------------------------------
// | PHP [ JUST YOU ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2017 http://www.jyphp.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: FlyingPig <flyingpigfp@163.com>
// +----------------------------------------------------------------------

namespace App\Model\Club;


use App\Model\Base;
use App\Model\User\User;
use Illuminate\Support\Facades\Redis;

class Room extends Base
{

    protected $table = 't_create_room_rec';

    protected $with = ['user'];

    protected $appends = ['online_count','status_string','type','special'];

    public function user()
    {
        return $this->hasOne(User::class,'id','uid')->select(['nick_name','id','avater_url']);
    }

    public function getOnlineCountAttribute()
    {
        return (int)Redis::hGet('ROOM_PLAYER_NUM',$this->id);
    }

    public function getStatusStringAttribute()
    {
        if ($this->status < 3) {
            return '准备中';
        }
        return '游戏中';
    }


    public function getTypeAttribute()
    {
        return explode('|',$this->rule)[0];
    }

    public function getSpecialAttribute()
    {
        return explode('|',$this->rule)[1];
    }

}